import { Component } from '@angular/core';
import { EntropyDetectSelfLearningDataService } from 'app/shared';
import { isEmpty, isNumber, trim } from 'lodash';
import { finalize } from 'rxjs';

@Component({
  selector: 'aui-learn-detail',
  templateUrl: './learn-detail.component.html',
  styleUrls: ['./learn-detail.component.less']
})
export class LearnDetailComponent {
  rowData;
  protectedObjectName: string;
  learningShow = false;
  fullCount = 0;
  incrementalCount = 0;

  constructor(
    private selfLearningDataService: EntropyDetectSelfLearningDataService
  ) {}

  resetResult() {
    this.learningShow = false;
  }

  getSelfLearning() {
    this.selfLearningDataService
      .querySelfLearningData({
        fileSystemId: this.rowData?.uuid,
        protectedObjName: trim(this.protectedObjectName)
      })
      .pipe(
        finalize(() => {
          this.learningShow = true;
        })
      )
      .subscribe({
        next: res => {
          this.fullCount = this.maxCountShow(res.fullBackupCount);
          this.incrementalCount = this.maxCountShow(res.incBackupCount);
        },
        error: () => {
          this.fullCount = 0;
          this.incrementalCount = 0;
        }
      });
  }

  maxCountShow(count) {
    if (!isNumber(count)) {
      return 0;
    }
    return count > 15 ? 15 : count;
  }
}
